<template>
  <div>
    <page-tools>
      <template v-slot:before>
        <el-row type="flex" justify="space-between" align="middle">
          <el-col>
            <el-row type="flex" justify="space-around" align="middle">
              <h5>目录名称</h5>
              <el-input v-model="input" placeholder="请输入内容" />
            </el-row>
          </el-col>
          <el-col>
            <el-row type="flex" justify="space-between" align="middle" style="padding-left:30px">
              <h5>状态</h5>
              <el-select v-model="page.state" placeholder="请选择">
                <el-option label="启用" :value="1" />
                <el-option label="禁用" :value="0" />
              </el-select>
              <!-- 下拉菜单 -->
              <el-button size="small" @click="clear">清除</el-button>
              <el-button size="small" type="primary" @click="search">搜索</el-button>
            </el-row>
          </el-col>
        </el-row>

      </template>
      <template v-slot:after>
        <div v-if="showLink" to="/subject/item" class="showLink" @click="downLink">
          <i class="el-icon-back" />
          返回学科
        </div>
        <el-button size="small" type="success" @click="showDialog(0)">
          <i class="el-icon-edit" style="color:#fff" />
          新增标签</el-button>
      </template>

    </page-tools>
    <i class="el-icon-info">数据一共{{ page.counts }}条</i>
    <!-- 表格 row-key 展开树形-->
    <el-table border :data="list" row-key="id">
      <el-table-column label="序号" type="index" />
      <el-table-column label="所属学科" prop="subjectName" />
      <el-table-column label="目录名称" prop="tagName" />
      <el-table-column label="创建者" prop="username" />
      <el-table-column label="创建日期" prop="addDate">
        <template slot-scope="{row}">
          {{ row.addDate | formatDate }}
        </template>
      </el-table-column>

      <el-table-column label="面试题数量" prop="totals" />
      <el-table-column label="状态" prop="state" :formatter="showFormat" />
      <el-table-column label="操作" width="250px">
        <template v-slot="{ row }">
          <el-button type="text" @click="disable(row)">{{
            row.state === 1 ? "禁用" : "启用"
          }}</el-button>
          <el-button
            type="text"
            :disabled="row.state === 1"
            @click="editTags(1,row)"
          >修改</el-button>
          <el-button
            type="text"
            :disabled="row.state === 1"
            @click="delTags(row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        background
        :page-sizes="[5, 10, 20, 50]"
        :page-size="page.pagesize"
        :current-page="page.page"
        layout=" prev, pager, next, sizes,jumper"
        :total="page.counts"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-row>
    <!-- 分页器 -->
    <!-- 新增弹框 -->
    <el-dialog
      :title=" changeTitle ==0 ?'新增标签':'修改标签' "
      :visible="dialogVisible"
      width="30%"
      @close="btnCancel"
    >
      <!-- 表单 -->
      <el-form ref="DirForm" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="所属学科">
          <el-select
            v-model="formData.subjectID"
            placeholder="请选择"
            style="width:100%"
          >
            <el-option
              v-for="item in selectList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input
            v-model="formData.tagName"
            style="width:100%"
            placeholder="请输入目录名称"
          />
        </el-form-item>
      </el-form>
      <!-- 表单 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>

        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增弹框 -->
    <el-card>
      <!-- header -->
      <el-row type="flex" justify="space-between" style="margin-bottom: 20px">
        <el-col :span="21">
          <el-form :inline="true">
            <el-form-item label="标签名称">
              <el-input
                size="mini"
                style="width: 200px; margin-right: 20px"
              />
            </el-form-item>
            <el-form-item label="状态">
              <el-select value="请选择" size="mini" style="width: 200px">
                <el-option value="item1" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <template>
                <el-button size="mini">清除</el-button>
                <el-button size="mini" type="primary">搜索</el-button>
              </template>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="3">
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
          >新增学科</el-button>
        </el-col>
      </el-row>

      <!-- info -->
      <el-alert title="数据一共21条" :closable="false" show-icon type="info" />

      <!-- 表格 -->
      <el-table style="width: 100%; margin-top: 20px">
        <el-table-column align="center" label="序号" type="index" width="width" />
        <el-table-column align="center" label="学科名称" width="width" />
        <el-table-column align="center" label="创建者" width="width" />
        <el-table-column align="center" label="创建日期" width="width" />
        <el-table-column align="center" label="前台是否显示" width="width" />
        <el-table-column align="center" label="二级目录" width="width" />
        <el-table-column align="center" label="标签" width="width" />
        <el-table-column align="center" label="题目数量" width="width" />
        <el-table-column align="center" label="操作" width="width">
          <template>
            <el-button type="text">学科分类</el-button>
            <el-button type="text">学科标签</el-button>
            <el-button type="text">修改</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import PageTools from '@/components/pageTools'
import { mapState } from 'vuex'
import { getCatalogue, resetCatalogue, simple, getCatalogueDetail } from '@/api/subject'
import { getTags, addTags, updateTags, delTags } from '@/api/label'
export default {
  name: '',
  components: {
    PageTools
  },
  props: {

  },
  data() {
    return {
      input: '',
      list: [],
      selectList: [],
      formData: {
        tagName: '',
        subjectID: ''
      },
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 页大小
        counts: 100, // 总记录数
        directoryName: '',
        state: null
      },
      changeTitle: 0,
      dialogVisible: false,
      rules: {
        subjectName: [{ required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        directoryName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      value: '',
      showTitle: 0

    }
  },
  computed: {
    ...mapState('subject', ['showLink']),
    subjects() {
      return this.$route.query || null
    }
  },
  watch: {

  },
  created() {
    this.getCatalogue()
    this.simple()
    this.getTags()
  },
  mounted() {

  },
  methods: {
    // 获取标签列表数据
    async getTags() {
      const res = await getTags(this.page)
      this.list = res.items
      this.page.counts = res.counts
    },
    async simple() {
      const data = await simple()
      this.selectList = data
    },
    // 删除
    async delTags(row) {
      await this.$confirm('此操作将永久删除该目录，是否继续？', {
        type: 'warning'
      }).then(async() => {
        await delTags(row)
        this.getTags()
        this.$message.success('删除成功！')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 确定
    async  btnOK(row) {
      if (this.formData.id) {
        await updateTags({
          id: this.formData.id,
          subjectID: this.formData.subjectID,
          tagName: this.formData.tagName
        })
      } else {
        await addTags({
          subjectID: this.formData.subjectID,
          tagName: this.formData.tagName
        })
        this.getTags()
      }

      this.$message.success('操作权限成功')
      this.dialogVisible = false
    },
    // 展示弹框
    async showDialog(can) {
      this.changeTitle = can
      this.dialogVisible = true
    },
    // 取消
    btnCancel() {
      this.dialogVisible = false
      this.$refs.DirForm.resetFields()
      this.formData = {
        directoryName: '',
        subjectID: ''
      }
    },
    // 获取目录数据
    async getCatalogue() {
      const res = await getCatalogue(this.page)
      this.list = res.items
      this.page.counts = res.counts
    },
    handleSizeChange(num) {
      this.page.page = num
      this.getCatalogue(this.page)
    },
    handleCurrentChange(num) {
      this.page.pagesize = num
      this.getCatalogue(this.page)
    },
    downLink() {
      this.$store.commit('subject/setShowlink', false)
      this.$router.push({ path: 'item' })
    },
    //  格式表格内容
    showFormat(row, column) {
      switch (row.state) {
        case 0:
          return '禁用'
          // break
        case 1:
          return '启用'
          // break
        default:
          return '未知'
      }
    },
    async disable(row) {
      try {
        await resetCatalogue({
          id: row.id,
          state: (row.state = row.state === 1 ? 0 : 1)
        })
        this.$message.success('操作成功')
      } catch (error) {
        this.$message.error('操作失败')
      }
    },
    // 修改
    async editTags(can, row) {
      this.showDialog(can)
      const data = await getCatalogueDetail(row.id)
      console.log(data)
      this.$refs.DirForm.formData = data
    },
    clear() {
      this.input = ''
      this.page.state = null
    },
    search() {
      this.page.page = 1
      this.getTags()
    }
  }
}
</script>

<style scoped lang='scss'>
h5{
  width: 60px;
}
.el-button--small{
 margin-left: 10px;
}
::v-deep .el-input__inner{
  width: 200px;
  height: 32px ;
  line-height: 32px;
  padding:0 20px;
}
::v-deep .el-input{
 width: 200px;
}
    i {
      margin-right: 5px;
      color: #999;
    }
   ::v-deep .el-pagination .el-select .el-input .el-input__inner{
      width: 120px;
        height:28px;
    }
   ::v-deep .el-pagination__editor.el-input .el-input__inner {
        width: 120px;

    }
.el-dropdown {
  width: 150px;
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  border: 1px solid rgb(228, 230, 236);
}
.el-dropdown-link{
  display: flex;
  justify-content: space-between;
  align-items: center;
  color:#ccc;
}
.showLink{
  .el-icon-back{
 color:rgb(107, 171, 255);
  }
  color:rgb(107, 171, 255);
display: flex;
align-items: center;
}
::v-deep .el-dialog__header{
  display: flex;
  align-items: center;

 background-color:rgb(64, 158, 255)
}
.selectItem{
  width: 250px;
}
.el-dropdown{
  width: 200px;
}
</style>
<style>
</style>
